<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class wilayah_model extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 30;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'A.POSITION';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$cari = isset($_POST['CARI']) ? strval($_POST['CARI']) : '';	
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('A.*');
			$this->db->select('B.NM_LEVEL');
		
			$this->db->from('R_WILAYAH A' );
			$this->db->join('R_WILAYAH_LEVEL B', 'B.LEVEL_ID = A.LEVEL');
			$where = "(upper(A.ID_WILAYAH) like upper('%$cari%') or upper(A.WILAYAH) like upper('%$cari%'))";
			$this->db->where($where);
			
		$this->db->order_by($sort, $order);
		
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
		
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('ID_WILAYAH',$kode);
		
		$this->db->select('*');
		$this->db->from('TS_WILAYAH');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		//$this->db->select();
		$this->db->from('TS_WILAYAH');
		$this->db->where('ID_WILAYAH', $kode);
		
		return $this->db->get();
	}
	
	function InsertOnDb($data){
		$this->db->flush_cache();
		$this->db->set('ID_WILAYAH', 	$data['ID_WILAYAH']);
		$this->db->set('WILAYAH', 	$data['WILAYAH']);
		$this->db->set('WLEVEL', 	$data['WLEVEL']);
		$this->db->set('REMARK', 	$data['REMARK']);
		//Ambil user id dari model auth (dengan kuid dari cookie)
		$this->db->set('CUID', $this->auth_model->getUID());
		// $this->db->set('I_DATE', date('Y-m-d H:i:s'));
		// $this->db->set('', $data['']);
		// $this->db->set('', $data['']);
		
		return $this->db->insert('R_WILAYAH');
		
	}
	
	function UpdateOnDb($data){
		$this->db->flush_cache();
		$this->db->set('ID_WILAYAH', 	$data['ID_WILAYAH']);
		$this->db->set('WILAYAH', 	$data['WILAYAH']);
		$this->db->set('WLEVEL', 	$data['WLEVEL']);
		$this->db->set('REMARK', 	$data['REMARK']);
		
		$this->db->set('CUID', $this->auth_model->getUID());
		//$this->db->set('I_DATE', date('Y-m-d H:i:s'));
		// $this->db->set('', $data['']);
		// $this->db->set('', $data['']);
		
		$this->db->where('ID_WILAYAH', 	$data['ID_WIL']);
		
		return $this->db->update('R_WILAYAH');
		
	}
	
	 function DeleteOnDb($kode){
		 //query delet data di db
                $this->db->flush_cache();
                $this->db->where('ID_WILAYAH', $kode);
              
                $result = $this->db->delete('R_WILAYAH');
               
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }
}

?>